package view.listeners;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JPanel;

import model.Constants;
import model.Subnet;
import view.components.SubnetComboBox;
import view.dialogs.AddIpByListDialog;
import view.views.dialogContentViews.AddIpByListPanel;
import view.views.dialogContentViews.IpListPanel;
import controller.Controller;

public class SubnetBoxListener implements ItemListener {

	private final AddIpByListDialog addIpByListDialog;
	private final JPanel ipListPanel;
	private final SubnetComboBox subnetCombo;
	private IpListPanel ipList;
	private final AddIpByListPanel content;

	public SubnetBoxListener(AddIpByListDialog addIpByListDialog,
			SubnetComboBox subnetCombo, JPanel ipListPanel, IpListPanel ipList, AddIpByListPanel content) {
		this.addIpByListDialog = addIpByListDialog;
		this.subnetCombo = subnetCombo;
		this. ipListPanel = ipListPanel;
		this.ipList = ipList;
		this.content = content;
	}

	@Override
	public void itemStateChanged(ItemEvent e) {
		if (e.getStateChange() == 2) {
			Subnet sub = null;
			if (!subnetCombo.getBox().getSelectedItem()
					.equals(Constants.EMPTY)) {
				sub = subnetCombo.getMap().get(
						subnetCombo.getBox().getSelectedItem());

				if (!(ipListPanel.getComponents().length == 0)) {
					ipListPanel.remove(0);
				}
				ipList = new IpListPanel(sub);
				Controller.getInstance().setIpList(ipList);
				ipListPanel.add(ipList);
					content.add(ipListPanel);
				
			} else {
				ipListPanel.remove(0);
			}

			addIpByListDialog.pack();

		}

	}

	}


